package com.qifan.energy.base;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qifan.energy.R;
//import com.shushan.base.ScreenManager;
import com.orhanobut.logger.Logger;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

public class BaseWebActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "BaseWebActivity";

    protected WebView mWebView;
    private ProgressBar progressBar;
    private TextView titleTextView;
    private ImageView backImg;
    private TextView backTextView;
    private TextView rightBackText;

    protected String title;
    protected String url;

    private static final boolean enableProgressBar = false;
    private boolean canBack = false;

    /*@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        *//*initWindow();*//*

        *//*setContentView(R.layout.activity_base_web);*//*
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_web;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initView();

        initWebView();

        title = getIntent().getStringExtra("title");
        if(!TextUtils.isEmpty(title)){
            titleTextView.setText(title);
        }
        url = getIntent().getStringExtra("url");
        url = "https://www.baidu.com/";
        Logger.i(TAG,"url=" + url);
        mWebView.loadUrl(url);
    }

    @Override
    protected void initData() {

    }

    /*private void initWindow() {
        ScreenManager screenManager;
        screenManager = ScreenManager.getInstance();
        screenManager.setStatusBar(true, this);
        screenManager.setScreenRoate(true, this);
        screenManager.setFullScreen(false, this);
    }*/

    private void initView() {
        mWebView = findViewById(R.id.base_web_webview);
        progressBar = findViewById(R.id.base_web_progress_bar);
        titleTextView = findViewById(R.id.base_web_title_tv);
        backImg = findViewById(R.id.base_web_left_back_img);
        backImg.setOnClickListener(this);
        backTextView = findViewById(R.id.base_web_title_back_tv);
        backTextView.setOnClickListener(this);
        rightBackText = findViewById(R.id.base_web_title_right_tv);
        rightBackText.setOnClickListener(this);
        if(canBack){
            rightBackText.setVisibility(View.VISIBLE);
        }else {
            rightBackText.setVisibility(View.GONE);
        }
    }

    private void initWebView() {
        WebSettings mWebSettings = mWebView.getSettings();
        mWebSettings.setSupportZoom(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setDefaultTextEncodingName("utf-8");
        mWebSettings.setLoadsImagesAutomatically(true);

        //调用JS方法.安卓版本大于17,加上注解 @JavascriptInterface
        mWebSettings.setJavaScriptEnabled(true);

        mWebView.setWebChromeClient(webChromeClient);
        mWebView.setWebViewClient(webViewClient);
    }

    WebViewClient webViewClient = new WebViewClient(){

        /**
         * 多页面在同一个WebView中打开，就是不新建activity或者调用系统浏览器打开
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    };

    WebChromeClient webChromeClient = new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView webView, int newProgress) {
            super.onProgressChanged(webView, newProgress);

            if(enableProgressBar){
                if(newProgress == 100){
                    progressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else{
                    progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar.setProgress(newProgress);//设置进度值
                }
            }
        }
    };

    protected void onLeftBackward() {
        Log.i(TAG,"onLeftBackward()");
        //返回上一级
        onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_web_left_back_img:
            case R.id.base_web_title_back_tv:
                goBack();
                break;
            case R.id.base_web_title_right_tv:
                onLeftBackward();
                break;
            default:
                break;
        }
    }

    private void goBack() {
        if(canBack){
            if(mWebView.canGoBack()){
                mWebView.goBack();
            }else {
                onLeftBackward();
            }
        }else {
            onLeftBackward();
        }

    }

    public boolean isCanBack() {
        return canBack;
    }

    public void setCanBack(boolean canBack1){
        canBack = canBack1;
    }
}
